﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;

namespace PowerWaveAddin
{
	public partial class PowerWaveRibbon : OfficeRibbon
	{
		public static PowerWaveRibbon Instance { get; private set; }

		private IList<RibbonControl> controlsToToggle;

		public delegate void ControlToggler(bool enabled);

        private delegate void SetUpdatedPresentationNamesDelegate(ICollection<string> pNames);

		private ControlToggler togglerInstance;

		public ControlToggler ButtonEnabledToggler
		{
			get { return togglerInstance; }
			set { togglerInstance = value; }
		}

		public PowerWaveRibbon()
		{
			InitializeComponent();

			this.togglerInstance = new ControlToggler(ControlTogglerImpl);

			controlsToToggle = new List<RibbonControl>(4); ;
			controlsToToggle.Add(this.emailTextBox);
			controlsToToggle.Add(this.presentationNameCombo);
			controlsToToggle.Add(this.publishButton);
			controlsToToggle.Add(this.presentButton);

			Instance = this;
		}

		private void PowerWaveRibbon_Load(object sender, RibbonUIEventArgs e)
		{

		}

		/// <summary>
		/// Click event handler for the publish button
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void publishButton_Click(object sender, RibbonControlEventArgs e)
		{
			string name = presentationNameCombo.Text;
			string email = emailTextBox.Text;

			if (!this.verifyWaveAccountFormat(ref email))
			{
				System.Windows.Forms.MessageBox.Show(email + " does not appear to be a Google Wave account.");
			}

			Globals.ThisAddIn.attemptExport(name, email);
		}

		/// <summary>
		/// Click event handler for the present button.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void presentButton_Click(object sender, RibbonControlEventArgs e)
		{
			ThisAddIn.postHelper = new SimplePostHelper(UrlStrings.PresentationServiceUrl);
			Globals.ThisAddIn.Application.ActiveWindow.Presentation.SlideShowSettings.Run();
		}

		private void email_changed(object sender, RibbonControlEventArgs e)
		{
			string email = this.emailTextBox.Text;
			if (this.verifyWaveAccountFormat(ref email))
			{
                if (!this.presentationNameDiscoveryWorker.IsBusy)
                {
                    this.presentationNameDiscoveryWorker.RunWorkerAsync(email);
                }
			}
		}

		private void presentationName_Changed(object sender, RibbonControlEventArgs e)
		{
			string name = this.presentationNameCombo.Text;
			bool alreadyHaveKey = Globals.ThisAddIn.presentationManager.ContainsName(name);
			if (alreadyHaveKey)
			{
				Globals.ThisAddIn.presentationManager.SetCurrentPresentationByName(name);
				this.presentButton.Enabled = true;
			}
			else
			{
				PresentationInfo pInfo = new PresentationInfo()
				{
					Name = name,
					Key = null
				};

				Globals.ThisAddIn.presentationManager.SetCurrentPresentation(pInfo);
			}
		}

		private void dialogLauncher_Click(object sender, RibbonControlEventArgs e)
		{
            UploadForm.Instance.Show();
		}

		public void ControlTogglerImpl(bool enabled)
		{
			foreach (RibbonControl c in this.controlsToToggle)
			{
				c.Enabled = enabled;
			}
		}

		/// <summary>
		/// Puts the address in the correct format
		/// </summary>
		/// <param name="address"></param>
		/// <returns></returns>
		private bool verifyWaveAccountFormat(ref string address)
		{
			// Check if it it ends with the proper domain 
			if (address.EndsWith(@"@googlewave.com"))
			{
				return true;
			}

			if (address.EndsWith(@"@gmail.com"))
			{
				int atPos = address.IndexOf('@');
				address.Substring(0, atPos);
				address += @"@googlewave.com";
				return true;
			}

			if (!address.Contains('@'))
			{
				address += @"@googlewave.com";
				return true;
			}

			return false;

		}

        private void presentationNameDiscoveryWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            ICollection<string> pNames = Globals.ThisAddIn.GetPresentationListForUser(e.Argument as string);
            this.presentationNameCombo.Items.Clear();
            if (pNames != null && pNames.Count > 0)
            {
                foreach (string p in pNames)
                {
                    RibbonDropDownItem item = new RibbonDropDownItem();
                    item.Label = p;
                    this.presentationNameCombo.Items.Add(item);
                }
            }
        }
	}
}
